package com.dis.api.config;

import com.dis.common.context.BaseContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

public class DefaultFeignConfig {
    // OpenFeign的日志
    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }

    // 注册一个拦截器，Openfeign实现微服务之间的用户信息传递
    // 还是跟网关一样，将调用者的用户信息发送到Openfeign拦截器的请求头里，这样common的公共拦截器就会解析请求头里的用户信息
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = BaseContext.getCurrentId();
                if(userId != null){
                    requestTemplate.header("user-info",userId.toString());
                }
            }
        };
    }

}
